package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.JSONUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 注册用户资源控制器
 */
@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;

    /**
     * 用户注册接口
     * @param user
     * @return
     */
    @PostMapping
    public JSONResult register(User user){
        JSONResult jsonResult = new JSONResult();
        try {
            userService.register(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
    /**
     * 修改用户信息接口
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    public JSONResult updateInfo(User user){
        JSONResult jsonResult = new JSONResult();
            userService.updateInfo(user);
            jsonResult.setResult(user);
        return jsonResult;
    }

    /**
     * 用户游记接口
     * @param qo
     * @return
     */
    @GetMapping("/{authorId}/travels")
    public PageInfo query(TravelQueryObject qo){
        qo.setOrderBy("lastUpdateTime desc");
        PageInfo pageInfo = travelService.query(qo);
        return pageInfo;
    }
}
